package com.dhcc.cdc.base.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.base.entity.CdcToolEntity;
import com.dhcc.cdc.base.service.ICdcToolService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 分析工具
 * 
 * @ClassName: CdcToolController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:19:37
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdctool")
public class CdcToolController extends BaseController {

	private String PREFIX = "/backend/cdc/cdctool/";

	@Autowired
	private ICdcToolService cdcToolService;
	

    /**
     * 跳转到分析工具列表首页
     */
    @RequestMapping("")
    @Permission("cdctool:list")
    public String index() {
        return PREFIX + "cdctool.html";
    }
	
    /**
     * 跳转到新增分析工具
     */
    @RequestMapping("/to_add")
    @Permission("cdctool:add")
    public String toAdd() {
        return PREFIX + "cdctool_add.html";
    }
	
	/**
     * 跳转到修改分析工具
     */
    @RequestMapping("/to_update/{cdcToolId}")
    @Permission("cdctool:update")
    public String toUpdate(@PathVariable Long cdcToolId, Model model) {
        CdcToolEntity cdcTool = this.cdcToolService.selectById(cdcToolId);
        model.addAttribute("cdcTool",cdcTool);
        return PREFIX + "cdctool_edit.html";
    }

    /**
     * 获取分析工具列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdctool:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcToolEntity> page = new PageFactory<CdcToolEntity>().defaultPage();
        List<CdcToolEntity> result = this.cdcToolService.page(page,params);
        page.setRecords((List<CdcToolEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增分析工具
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdctool:add")
    @BussinessLog(value = "新增分析工具")
    public Object add(CdcToolEntity cdcTool) {
        if (CommonUtil.isOneEmpty(cdcTool)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcTool.insert();
        return R.ok();
    }

    /**
     * 删除分析工具
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdctool:delete")
    @BussinessLog(value = "删除分析工具")
    public Object delete(@RequestParam Long cdcToolId) {
	
        this.cdcToolService.deleteById(cdcToolId);

        return R.ok();
    }

    /**
     * 修改分析工具
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdctool:update")
    @BussinessLog(value = "修改分析工具")
    public Object update(CdcToolEntity cdcTool) {
        if (CommonUtil.isOneEmpty(cdcTool)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcToolEntity old = this.cdcToolService.selectById(cdcTool.getId());

        old.setCode(cdcTool.getCode());
        old.setDescription(cdcTool.getDescription());
        old.setServerIp(cdcTool.getServerIp());
        old.setServerPort(cdcTool.getServerPort());
        old.setServerPath(cdcTool.getServerPath());
        old.setNote(cdcTool.getNote());
        old.setIsActive(cdcTool.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
